package filter;

import pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录拦截器
 *
 * @author HLF
 * @date 2022/6/15 11:54
 */
@WebFilter(value = {
        "/fieldManage.jsp",
        "/index.jsp",
        "/noticeManage.jsp",
        "/orderManage.jsp",
        "/userManage.jsp",
        "/common.jsp"
})
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();

        User user = (User) session.getAttribute("user");
        if (user == null) {
            req.setAttribute("msg", "未登录，请先登录！");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        } else {
            chain.doFilter(request, response);
        }
    }
}
